<?php
/*
+----------------------------------------------------------------------+
| (c) Copyright Silkwires. 					                           |
| 	All Rights Reserved.                                               |
+----------------------------------------------------------------------+
| > Module: .........                                                  |
| > Author: (SW Team)  ........  					                   |
| >	Class description	: Defined data that is retrieved from database,| 
						  prepare some methods from base object        |
+----------------------------------------------------------------------+
*/

class Motor{
	
	private $db=null;
	private $config_site=null;
	
	function __construct(){
		$this->db=Zend_Registry::get('db');	
		$this->config_site=Zend_Registry::get('config_site');
	}
	
	function __destruct(){
		unset($this->db);
	}
	
	function insert($arrData){
        $arrData['created_time']=date('Y-m-d H:i:s');
        $arrData['updated_time'] =date('Y-m-d H:i:s');
		$rs=$this->db->insert('motor',$arrData);
		return $this->db->lastInsertId('motor');
	}
	
	function getMotor($order='id', $sort='DESC', $offset=0, $size='all') {
		
		$SQL="SELECT * FROM motor ".Common::getOrderBY($order,$sort,$offset,$size);
		$rs=$this->db->fetchAll($SQL);
		return $rs;
	}
    function countPostbyUser($user_id){
        $SQL="SELECT count(*) FROM motor where user_id=".$user_id;
		$rs=$this->db->fetchOne($SQL);
		return $rs;
    }
    /**
     * Get List Manufacture
     * @param $arrData
     * 
     */
     
    function getManufacture($type=0, $order='id', $sort='DESC', $offset=0, $size='all') {
        //debug_print_backtrace();
		$query = "SELECT * FROM manufacture WHERE type=$type AND id>0 ".Common::getOrderBY($order,$sort,$offset,$size);
		//$SQL1="SELECT * FROM manufacture WHERE type=$type AND name='Khác'";
        //print $query;die;
		$rs=$this->db->fetchAll($query);
        
        if ($type) $alias ='oto-khac';
        else $alias ='xm-khac';
        $rs[] = array (
            'id'=>0,
            'type'=>$type,
            'alias'=>$alias,
            'name'=>'Khác'
        );
        
        
        //$rs1 =array ();
		//$rs1=$this->db->fetchAll($SQL1);
		//return array_merge($rs,$rs1);
        return $rs;
	}
    /**
     * Get List Model
     * @param $arrData
     * 
     */
    function getModelIdByAlias($alias){
        //var_dump($alias);die();
        $SQL="SELECT id FROM model WHERE alias ='$alias' " ;
        $rs=$this->db->fetchOne($SQL);
        return $rs;
        
     }
    function getModel($type=0,$order='id', $sort='DESC', $offset=0, $size='all') {
        $SQL="SELECT * FROM model WHERE type=$type".Common::getOrderBY($order,$sort,$offset,$size);
        //$SQL1="SELECT * FROM model WHERE type=$type AND name='Khác'";
        $rs=$this->db->fetchAll($SQL);
        //$rs1=$this->db->fetchAll($SQL1);
        
        if ($type) $alias ='model-oto-khac';
        else $alias ='model-xm-khac';
        $rs[] = array (
            'id'=>0,
            'type'=>$type,
            'alias'=>$alias,
            'name'=>'Khác'
        );
        
        return $rs;
	}
    function getModelByManuId($manu_id,$order='id', $sort='DESC', $offset=0, $size='all') {
		$manu_id=intval($manu_id);
		$SQL="SELECT * FROM model WHERE manu_id=$manu_id ".Common::getOrderBY($order,$sort,$offset,$size);
		$rs=$this->db->fetchAll($SQL);
		return $rs;
	} 
 
    function getModelByManuAlias($manu_alias,$order='id', $sort='DESC', $offset=0, $size='all') {
		$SQL="SELECT
                model.id,
                model.`name`,
                model.alias,
                model.type,
                model.manu_id,
                model.cat_id,
                model.lang_id,
                model.admin_id,
                model.created_time,
                model.created_by,
                model.updated_time,
                model.updated_by,
                model.sort,
                model.active
                FROM
                manufacture
                INNER JOIN model ON manufacture.id = model.manu_id
                WHERE
                
                 manufacture.alias = '$manu_alias'".Common::getOrderBY($order,$sort,$offset,$size);
		$rs=$this->db->fetchAll($SQL);

		return $rs;
	}    
    /**
     * Get List Price
     * @param $arrData
     * 
     */
    function getPrice($type=0,$order='id', $sort='DESC', $offset=0, $size='all') {
		
		$SQL="SELECT * FROM price WHERE type=$type ".Common::getOrderBY($order,$sort,$offset,$size);
		$rs=$this->db->fetchAll($SQL);
		return $rs;
	}
    
    function getStyle($order='id', $sort='DESC', $offset=0, $size='all') {
		$SQL="SELECT * FROM style WHERE  id >0 ".Common::getOrderBY($order,$sort,$offset,$size);
		//$SQL1="SELECT * FROM style WHERE  name='Khác'";
		$rs=$this->db->fetchAll($SQL);
		//$rs1=$this->db->fetchAll($SQL1);
        
        
        $rs[] = array (
            'id'=>0,
            'alias'=>'khac',
            'name'=>'Khác'
        );
        
		return $rs;
	}
	/**
	 * Update Motor
	 * @param $arrData
	 * @param $id
	 */
	function updateMotor($arrData,$id){
        $arrData['updated_time'] =date('Y-m-d H:i:s');
		$rs=$this->db->update('motor',$arrData,'id='.$id);
		return $rs;
	}
   
	function getCity($order='id', $sort='DESC', $offset=0, $size='all') {
	
		if (strpos ($order, 'name')!== false){
            $SQL = "SELECT * FROM city ORDER BY TRIM(REPLACE(LOWER($order), 'đ', 'd')) $sort";
            if($size!='all') {
			     $SQL =" LIMIT $offset, $size";
		      };
		}
        else{
		  $SQL = "SELECT * FROM city ".Common::getOrderBY($order, $sort, $offset, $size);
        }
	
		$rs=$this->db->fetchAll($SQL);
		return $rs;
	}
	
	function getMotorByID ($id, $user_id){
	   $query = "SELECT * FROM motor WHERE user_id=? AND id=?";
       return $this->db->fetchRow ($query, array ($user_id, $id));
	}
    function delete($uid, $id){
        return $this->db->delete('motor','user_id='.$uid.' AND id='.$id);
    }	
}

?>